package com.huifer.wgx.service.impl;

import com.huifer.wgx.model.FunctionEntity;
import com.huifer.wgx.model.PersonnelEntity;
import com.huifer.wgx.repository.FunctionRepository;
import com.huifer.wgx.repository.PersonnelRepository;
import com.huifer.wgx.service.PersonnelService;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Service;

/**
 * <p>Title : PersonnelServiceImpl </p>
 * <p>Description : </p>
 *
 * @author huifer
 * @date 2019-03-18
 */
@Service
public class PersonnelServiceImpl implements PersonnelService {

    @Autowired
    private FunctionRepository functionRepository;
    @Autowired
    private PersonnelRepository personnelRepository;

    /**
     * 人员列表
     *
     * @return 人员列表
     */
    @Override
    public List<PersonnelEntity> list() {
        return personnelRepository.findAll();
    }

    @Override
    public PersonnelEntity saveOne(String name, Long funcID) {
        try {
            FunctionEntity functionEntity = functionRepository.findById(funcID).get();
            PersonnelEntity s = new PersonnelEntity();
            s.setPersonnelName(name);
            s.setFunctionId(functionEntity.getFunctionId());

            return personnelRepository.save(s);


        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }

    /**
     * 更新人员职能
     *
     * @param name 人员名称
     * @param funcid 新的职能id
     * @return 人员 {@link PersonnelEntity}
     */
    @Override
    public PersonnelEntity updatePersonnel(String name, Long funcid) {
        try {
            PersonnelEntity byPersonnelName = personnelRepository.findByPersonnelName(name);
            PersonnelEntity save = null;
            if (byPersonnelName != null) {
                FunctionEntity functionEntity = functionRepository.findById(funcid).get();
                PersonnelEntity personnelEntity = byPersonnelName;
                personnelEntity.setFunctionId(functionEntity.getFunctionId());
                save = personnelRepository.save(personnelEntity);
                return save;
            } else {
                return null;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 创建人员
     *
     * @param personnel 人员
     * @return 人员 {@link PersonnelEntity}
     */
    @Override
    public PersonnelEntity savePersonnel(PersonnelEntity personnel) {

        try {
            List<PersonnelEntity> byPersonnelNameAndFunctionId = personnelRepository
                    .findByPersonnelNameAndFunctionId(personnel.getPersonnelName(),
                            personnel.getFunctionId());

            FunctionEntity functionEntity = functionRepository.findById(personnel.getFunctionId())
                    .get();

            if (functionEntity != null) {
                if (byPersonnelNameAndFunctionId.size() == 0) {
                    return personnelRepository.save(personnel);
                } else {
                    return null;
                }
            } else {
                return null;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }

    /**
     * 查询所有人员
     *
     * @return 人员列表
     */
    @Override
    public Page<PersonnelEntity> findAll(Integer pageNum, Integer pageSize) {

        Sort sort = new Sort(Direction.DESC, "personnelId");
        Pageable pageable = PageRequest.of(pageNum - 1, pageSize, sort);
        Page<PersonnelEntity> all = personnelRepository.findAll(pageable);
        return all;
    }
}
